package com.hngy.xpq.dianming;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.Random;

public class MainActivity4 extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemSelectedListener {

    TextView text;
    Button bin;
    Spinner spr1,spr2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main4);

        text = findViewById(R.id.text_main_4);
        bin = findViewById(R.id.bin_main_4);
        spr1 = findViewById(R.id.sp_mian_banji);
        spr2 = findViewById(R.id.sp_mian_quan);

        bin.setOnClickListener(this);
        spr1.setOnItemSelectedListener(this);
        spr2.setOnItemSelectedListener(this);
        text.setMovementMethod(ScrollingMovementMethod.getInstance());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bin_main_4:
                Intent intent = new Intent(MainActivity4.this , MainActivity_lord.class);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

        String[] strXingYun1 = getResources().getStringArray(R.array.yy19_1);
        String[] strXingYun2 = getResources().getStringArray(R.array.yy19_2);
        String b = "";

        int indexquan = spr2.getSelectedItemPosition();
        int indexbanji = spr1.getSelectedItemPosition();
        int[] index;
        int kelang = 0;
        index = new int[indexquan];

        if( indexbanji == 1 && indexquan != 0){
            for(int x = 0; x < indexquan; x++) {
                Random XingYun = new Random();
                kelang = XingYun.nextInt(42) + 1;
                for (int y = 0; y < indexquan; y++) {
                    if(index[y] == kelang){
                        kelang = XingYun.nextInt(42) + 1;
                        y = 0;
                    }
                }
                index[x] = kelang;
                String a = String.valueOf(kelang);
                if(kelang<10){
                    a = "0" + a;
                }
                text.setText(b + "        学号： " + a + "  姓名：  " + strXingYun1[kelang] + '\n');
                b = text.getText().toString();
            }
        } else if( indexbanji == 2 && indexquan != 0){
            for(int x = 0; x < indexquan; x++) {
                Random XingYun = new Random();
                kelang = XingYun.nextInt(39) + 1;
                for (int y = 0; y < indexquan; y++) {
                    if(index[y] == kelang){
                        kelang = XingYun.nextInt(39) + 1;
                        y = 0;
                    }
                }
                index[x] = kelang;
                String a = String.valueOf(kelang);
                if(kelang<10){
                    a = "0" + a;
                }
                text.setText(b + "        学号： " + a + "  姓名：  " + strXingYun2[kelang] + '\n');
                b = text.getText().toString();
            }
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
}